<?php

namespace app\model;

use app\common\exception\LogicException;
use think\Model;

class UserToken extends Model
{
    /**
     * @Description
     * @author: zj
     * @param string $token
     * @time 2023/09/22 16:44:56
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkUserToken(string $token)
    {
        $token = $this->field('id,token')
            ->where('token',$token)
            ->find();
        if (!$token) {
            throw new LogicException('登录已过期，请重新登录',4101);
        }
        return true;
    }

    /**
     * @Description 更新用户token
     * @author: zj
     * @param int $id
     * @param string $token
     * @time 2023/08/29 09:48:12
     * @return true
     */
    public function updateToke(int $id,string $token)
    {
        $this->where('id',$id)
            ->update([
                'token' => $token
            ]);
        return true;
    }

    /**
     * @Description 新增用户token
     * @author: zj
     * @param int $userId
     * @param string $token
     * @time 2023/08/29 09:47:56
     * @return true
     */
    public function addToken(int $userId,string $token)
    {
        $this->insert([
            'user_id' => $userId,
            'token' => $token
        ]);

        return true;
    }
}